$(function () {
    // $("#type").trigger();
    changeFunction();
});


function changeFunction() {
    var type = $("#type").val();
    if (type == 1) {
        $("#url").parent().parent().show();
        $("#sourceName").parent().parent().hide();
        $("#sourceServer").parent().parent().hide();
        // $("#supplierType").val(11);
    }
    else if (type == 2) {
        $("#url").parent().parent().hide();
        $("#sourceName").parent().parent().show();
        $("#sourceServer").parent().parent().show();
        // $("#supplierType").val(21);
    }
    else if (type == 3) {
        $("#url").parent().parent().hide();
        $("#sourceName").parent().parent().hide();
        $("#sourceServer").parent().parent().hide();
        // $("#supplierType").val(31);
    }
    $.ajax({
        type: 'post',
        async: true,
        url: '/supplier/getSupplierTypeByMessageType',
        data: "type=" + type,
        success: function (data) {
            // alert(JSON.stringify(data));
            if (data.code == "0000") {
                for (var i = 0; i < data.data.length; i++) {
                    $("#supplierType").empty();
                    $("#supplierType").append("<option value='"+data.data[i].key+"'>"+data.data[i].value+"</option>")
                }
            } else {
                alert("查supplierType失败");
                return false;
            }
        }
    });
}

function update() {
    var param = "";
    var id = $("#id").val();
    var supplierType = $("#supplierType").val();
    var sysVersion = $("#sysVersion").val();
    var type = $("#type").val();
    var name = $("#name").val();
    if (name == "") {
        alert("请输入供应商名称");
        return false;
    }
    var timesLimitPerDay = $("#timesLimitPerDay").val();
    if (isNaN(timesLimitPerDay) || !(/(^[1-9]\d*$)/.test(timesLimitPerDay))) {
        alert("次数限制应为正整数");
        return false;
    }
    var outerAppKey = $("#outerAppKey").val();
    if (outerAppKey == "") {
        alert("请输入外部appId");
        return false;
    }
    var outerSecret = $("#outerSecret").val();
    if (outerSecret == "") {
        alert("请输入外部系统密钥");
        return false;
    }
    param = "type=" + type + "&supplierType=" + supplierType + "&name=" + name + "&timesLimitPerDay=" + timesLimitPerDay + "&outerAppKey="
        + outerAppKey + "&outerSecret=" + outerSecret + "&id=" + id + "&sysVersion=" + sysVersion;
    if (type == 1) {
        var url = $("#url").val();
        if (url == "") {
            alert("请输入Url");
            return false;
        }
        param = param + "&url=" + url;
    }
    else if (type == 2) {
        var sourceName = $("#sourceName").val();
        if (sourceName == "") {
            alert("请输入发送源名称");
            return false;
        }
        param = param + "&sourceName=" + sourceName;
        var sourceServer = $("#sourceServer").val();
        if (sourceServer == "") {
            alert("请输入发送服务器");
            return false;
        }
        param = param + "&sourceServer=" + sourceServer;
    }
    $.ajax({
        type: 'post',
        url: '/supplier/saveOrUpdate',
        data: param,
        success: function (data) {
            if (data.code == "0000") {
                alert(data.msg);
                window.location.href = "/supplier/list";
            } else if (data.code == "0001") {
                alert("添加失败,原因:" + data.msg);
                return false;
            } else if (data.code == "0002") {
                alert("修改失败,原因:" + data.msg);
                return false;
            } else if (data.code == "0003") {
                alert("保存失败,原因:" + data.msg);
                return false;
            } else {
                alert("保存失败");
                return false;
            }
        }
    });

}